package com.etime.shycourse.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.ListMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.config.PageMessage;
import com.etime.shycourse.dto.OrgTeacherDtoS;
import com.etime.shycourse.dto.UserTeacherDto;
import com.etime.shycourse.pojo.Course;
import com.etime.shycourse.pojo.SysUser;
import com.etime.shycourse.pojo.Teacher;
import com.etime.shycourse.services.interfaces.CourseServices;
import com.etime.shycourse.services.interfaces.OrgTeacherService;
import com.etime.shycourse.services.interfaces.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wb
 * @since 2021-12-14
 */
@Api(tags = "后台-机构教师管理-数据接口")
@RestController
@RequestMapping("/sys/org/teacher")
//@CrossOrigin 跨域
public class OrgTeacherController {
    @Autowired
    private OrgTeacherService service;
    @Autowired
    private SysUserService userService;
    @Autowired
    private CourseServices cServices;

    @ApiOperation("机构教师分页查询列表  可使用用户名查找")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "usertruename", value = "usertruename 可选择条件姓名"),
            @ApiImplicitParam(name = "userid", value = "userid 必填选择条件用户id"),
            @ApiImplicitParam(name = "pagenum", value = "第几页"),
            @ApiImplicitParam(name = "pagesize", value = "每页多少行"),
    })
    @GetMapping("/page")
    public PageMessage<OrgTeacherDtoS> selectOrgTeacherDtoByPage(OrgTeacherDtoS orgTeacherDtoS, Long pagenum, Long pagesize, HttpServletRequest request) {
        String token = request.getHeader("token");
        PageMessage<OrgTeacherDtoS> msg = new PageMessage<>();
        Page<OrgTeacherDtoS> orgTeacherDtoPage = service.selectOrgTeacherDtoByPage(orgTeacherDtoS, pagenum, pagesize, token);
        if (orgTeacherDtoPage != null) {
            msg.setCode(1);
            msg.setPagedata(orgTeacherDtoPage);
        } else {
            msg.setCode(0);
            msg.setMsg("没有查询到数据");
        }
        return msg;
    }

    @ApiOperation("添加机构教师 注意：中文注释的才可以添加 修改为token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "注意添加数据 加了中文注释的才可以添加"),
            @ApiImplicitParam(value = "密码", name = "userpwd"),
            @ApiImplicitParam(value = "专业", name = "usermajor"),
            @ApiImplicitParam(value = "姓名", name = "username"),
            @ApiImplicitParam(value = "联系电话", name = "userphone"),
            @ApiImplicitParam(value = "Email", name = "useremail"),
            @ApiImplicitParam(value = "照片", name = "showpic"),
            @ApiImplicitParam(value = "教师Title", name = "tectitle"),
            @ApiImplicitParam(value = "技术标签", name = "teclabel"),
            @ApiImplicitParam(value = "教师简介", name = "tecdesc"),
    })
    @PostMapping("/insert")
    public BaseMessage insertSysUser(OrgTeacherDtoS orgTeacherDtoS, HttpServletRequest request) {
        BaseMessage msg = new BaseMessage();
        String token = request.getHeader("token");
        int rtn = service.insertOrgTeacherDto(orgTeacherDtoS, token);
        msg.setCode(rtn);
        if (rtn > 0) {
            msg.setMsg("增加用户成功");
        } else {
            msg.setMsg("增加用户失败");
        }
        return msg;
    }

    @ApiOperation("修改机构教师 第一步")
    @ApiImplicitParam("第一步 根据tecId 获取用户详细信息")
    @GetMapping("/upGet/{tecId}")
    public PojoMessage<OrgTeacherDtoS> selectOrgUserTeacherById(@PathVariable Integer tecId) {
        PojoMessage<OrgTeacherDtoS> msg = new PojoMessage<>();
        OrgTeacherDtoS orgTeacherDtoS = userService.selectOrgUserTeacherById(tecId);
        if (orgTeacherDtoS != null) {
            msg.setCode(1);
            msg.setData(orgTeacherDtoS);
        } else {
            msg.setCode(0);
            msg.setMsg("没有找到用户");
        }
        return msg;
    }

    @ApiOperation("修改机构教师 第二步  ")
    @ApiImplicitParam("第二步 根据用户id和教师id 更新用户、教师信息")
    @PostMapping("/updateOrgUsTe")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "注意修改数据 加了中文注释的才可以修改"),
            @ApiImplicitParam(value = "姓名", name = "usertruename"),
            @ApiImplicitParam(value = "教师id", name = "tecid"),
            @ApiImplicitParam(value = "用户id", name = "userid"),
            @ApiImplicitParam(value = "专业", name = "usermajor"),
            @ApiImplicitParam(value = "联系电话", name = "userphone"),
            @ApiImplicitParam(value = "Email", name = "useremail"),
            @ApiImplicitParam(value = "教师Title", name = "tectitle"),
            @ApiImplicitParam(value = "技术标签", name = "teclabel"),
            @ApiImplicitParam(value = "照片", name = "showpic"),
            @ApiImplicitParam(value = "教师简介", name = "tecdesc"),
    })
    public BaseMessage updateOrgUserTeacher(OrgTeacherDtoS dto) {
        BaseMessage msg = new BaseMessage();
        int i = userService.updateOrgUserTeacher(dto);
        if (i != 0) {
            msg.setCode(1);
        } else {
            msg.setCode(0);
            msg.setMsg("修改机构教师失败");
        }
        return msg;
    }

    @ApiOperation("教师离职管理可以用 查询同机构的所有的教师 ")
    @ApiImplicitParam("根据机构id （tecOrgId）询本机构所有在职老师")
    @GetMapping("/list/{tecOrgId}")
    public ListMessage<UserTeacherDto> selectTeacherListByIds(@PathVariable Integer tecOrgId) {
        ListMessage<UserTeacherDto> msg = new ListMessage<>();
        List<UserTeacherDto> data = service.selectListByTecOrgId(tecOrgId);
        if (data != null) {
            msg.setCode(1);
            msg.setDatalist(data);
        } else {
            msg.setCode(0);
            msg.setMsg("没有找到同机构教师数据");
        }
        return msg;
    }

    @ApiOperation("教师离职管理可用 修改课表中的：课程教师id和课程教师姓名两个字段")
    @ApiImplicitParams({
            @ApiImplicitParam("根据course中的课程id（courseid"),
            @ApiImplicitParam(name = "修改课表中的：课程教师id和课程教师姓名两个字段"),
            @ApiImplicitParam(value = "必传值 课程教师姓名", name = "courseteachername"),
            @ApiImplicitParam(value = "必传值 课程教师id", name = "courseteacherid"),
            @ApiImplicitParam(value = "必传值 课程id", name = "courseid"),
    })
    @PostMapping("/up")
    public BaseMessage updateCourses(Course course) {
        BaseMessage msg = new BaseMessage();
        int i = cServices.updateCourse(course);
        if (i != 0) {
            msg.setCode(1);
            msg.setMsg("修改教师成功");
        } else {
            msg.setCode(0);
            msg.setMsg("修改教师失败");
        }
        return msg;
    }

    /**
     * 删除 本质是：修改教师表->教师所属机构id=-2。变更为独立教师身份。
     * 根据教师表 中的教师id
     */
    @ApiOperation("教师离职管理可以用 删除本质是：修改教师表->教师所属机构id=-2。变更为独立教师身份。")
    @ApiImplicitParam("根据教师表 中的教师id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "修改教师表->教师所属机构id=-2"),
            @ApiImplicitParam(value = "教师id", name = "tecid"),
    })
    @PostMapping("/delete")
    public BaseMessage deleteUpDataTeacher(Teacher teacher) {
        BaseMessage msg = new BaseMessage();
        int i = cServices.upDataTeacher(teacher);
        if (i != 0) {
            msg.setCode(1);
            msg.setMsg("教师离职成功");
        } else {
            msg.setCode(0);
            msg.setMsg("教师离职失败");
        }
        return msg;
    }


    @ApiOperation("获取教师信息 根据用户id")
    @GetMapping("/user/ById/{userId}")
    public PojoMessage<OrgTeacherDtoS> selectOrgUserById(@PathVariable Integer userId) {
        PojoMessage<OrgTeacherDtoS> msg = new PojoMessage<>();
        OrgTeacherDtoS data = userService.selectOrgUserById(userId);
        if (data != null) {
            msg.setCode(1);
            msg.setData(data);
        } else {
            msg.setCode(0);
            msg.setMsg("没有找到用户");
        }
        return msg;
    }

    @ApiOperation("让用户成为教师")
    @ApiImplicitParam("修改用户所属机构 创建教师信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id", name = "userid"),
    })
    @PostMapping("/upUser/CreateTeacher")
    public BaseMessage upUserCreateTeacher(UserTeacherDto userTeacherDto) {
        BaseMessage msg = new BaseMessage();
        int i = userService.upUserCreateTeacher(userTeacherDto);
        if (i != 0) {
            msg.setCode(1);
            msg.setMsg("成为教师成功");
        } else {
            msg.setCode(0);
            msg.setMsg("成为教师失败");
        }
        return msg;
    }

    @ApiOperation("修改教师的信息 ,后台编辑用户中编辑教师")
    @PostMapping("/update/Teacher")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "注意修改数据 加了中文注释的才可以修改"),
            @ApiImplicitParam(value = "教师id", name = "tecid"),
            @ApiImplicitParam(value = "专业", name = "usermajor"),
            @ApiImplicitParam(value = "教师Title", name = "tectitle"),
            @ApiImplicitParam(value = "技术标签", name = "teclabel"),
            @ApiImplicitParam(value = "教师简介", name = "tecdesc"),
    })
    public BaseMessage upDataTeacher(UserTeacherDto dto) {
        BaseMessage msg = new BaseMessage();
        int i = userService.upDataTeacher(dto);
        if (i != 0) {
            msg.setCode(1);
        } else {
            msg.setCode(0);
            msg.setMsg("修改机构教师失败");
        }
        return msg;
    }

    @ApiOperation("根据用姓名模糊查询 用户信息")
    @GetMapping("/LikeName/{name}")
    public ListMessage<SysUser> selectSysUserLikeName(@PathVariable String name) {
        ListMessage<SysUser> msg = new ListMessage<>();
        List<SysUser> data = userService.selectSysUserLikeName(name);
        if (data != null) {
            msg.setCode(1);
            msg.setDatalist(data);
        } else {
            msg.setCode(0);
            msg.setMsg("没有用户数据");
        }
        return msg;
    }

    @ApiOperation("验证用户名是否存在;code=0:不存在；code=1：存在")
    @ApiImplicitParam("用户名")
    @GetMapping("/check/{username}")
    public PojoMessage checkusername(@PathVariable("username") String username) {
        PojoMessage<SysUser> msg = new PojoMessage();
        SysUser user = new SysUser();
        user.setUsername(username);
        List<SysUser> list = userService.selectUserList(user);
        if (list != null && list.size() > 0) {
            msg.setCode(1);
            msg.setData(list.get(0));
            msg.setMsg("用户名已存在");
        } else {
            msg.setCode(0);
            msg.setMsg("用户名不存在，可以使用");
        }
        return msg;
    }
}
